<template>
  <div class="sysDialog">
    <!-- 修改查看系统参数 -->
    <el-row>
      <el-form
        ref="elForm"
        class="elFormTxtL"
        :model="confPropInfo"
        :rules="rulesSys"
        size="medium"
        label-width="135px"
      >
        <div class="titleParame">基本参数</div>

        <el-col :span="24">
          <el-col :span="8">
            <el-form-item label="网关IP地址" prop="gateway_ip">
              <el-input
                v-model="confPropInfo.gateway_ip"
                placeholder="网关IP地址"
                clearable
                :style="{ width: '100%' }"
              />
            </el-form-item>
          </el-col>
          <el-col v-show="false" :span="8">
            <el-form-item label="hess文件地址" prop="hess_json_file">
              <el-input
                v-model="confPropInfo.hess_json_file"
                disabled
                placeholder="hess文件地址"
                clearable
                :style="{ width: '100%' }"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="客户端端口" prop="agent_port">
              <el-input
                v-model="confPropInfo.agent_port"
                :min="1"
                controls-position="right"
                placeholder="agent端口"
                clearable
                :style="{ width: '100%' }"
              />
            </el-form-item>
          </el-col>
        </el-col>

        <div class="titleParame">线程参数</div>
        <div style="display: inline-block; width: 100%">
          <!-- 	<el-col :span="8">
							<el-form-item label="时间间隔 (秒)" prop="agent_check_interval">
								<el-input-number :min="0" controls-position="right" v-model="confPropInfo.agent_check_interval" placeholder="端口过期时间"
								 clearable :style="{ width: '100%' }" />
							</el-form-item>
						</el-col> -->
          <el-col :span="8">
            <el-form-item label="节点检查间隔 (秒)" prop="ping_interval">
              <el-input-number
                v-model="confPropInfo.ping_interval"
                :min="0"
                controls-position="right"
                placeholder="节点检查间隔"
                clearable
                :style="{ width: '100%' }"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item
              label="业务检查间隔 (秒)"
              prop="business_check_interval"
            >
              <el-input-number
                v-model="confPropInfo.business_check_interval"
                :min="0"
                controls-position="right"
                placeholder="业务检查间隔"
                clearable
                :style="{ width: '100%' }"
              />
            </el-form-item>
          </el-col>
        </div>

        <div class="titleParame">网络超时</div>
        <div style="display: inline-block; width: 100%">
          <el-col :span="8">
            <el-form-item
              label="http超时设置 (秒)"
              prop="httptest_connection_timeout"
            >
              <el-input-number
                v-model="confPropInfo.httptest_connection_timeout"
                :min="0"
                controls-position="right"
                placeholder="http检查连接超时设置"
                clearable
                :style="{ width: '100%' }"
              />
            </el-form-item>
          </el-col>
          <el-col :span="16">
            <el-form-item label="快速检测" required>
              <el-tooltip
                class="item"
                effect="dark"
                content="保障节点检查重要节点是否在线时，开启该选项将不通过控制台再次检查重要节点是否在线"
                placement="top"
              >
                <i style="margin-right: 6px" class="el-icon-info" />
              </el-tooltip>

              <el-switch
                v-model="confPropInfo.ping_quick_check"
                active-value="true"
                inactive-value="false"
              />
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="RPC请求线程数 " prop="agent_rpc_thread">
              <el-input-number
                v-model="confPropInfo.agent_rpc_thread"
                :min="1"
                controls-position="right"
                placeholder="http检查连接超时设置"
                clearable
                :style="{ width: '100%' }"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="RPC超时毫秒数" prop="agent_rpc_timeout">
              <el-input-number
                v-model="confPropInfo.agent_rpc_timeout"
                :min="1000"
                controls-position="right"
                placeholder="http检查连接超时设置"
                clearable
                :style="{ width: '100%' }"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="TCP超时毫秒数" required>
              <el-input-number
                v-model="confPropInfo.tcp_connect_timeout"
                :min="0"
                controls-position="right"
                placeholder="tcp检查连接超时设置"
                clearable
                :style="{ width: '100%' }"
              />
            </el-form-item>
          </el-col>
        </div>

        <div class="titleParame">数据同步</div>
        <div style="display: inline-block; width: 100%">
          <el-col :span="8">
            <el-form-item label="持续写入模式" required>
              <el-switch
                v-model="confPropInfo.continueWriteStatus"
                active-value="true"
                inactive-value="false"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="延迟关闭文件锁" required>
              <el-input-number
                v-model="confPropInfo.closeFileTime"
                :min="0"
                :max="20"
                :disabled="
                  confPropInfo.continueWriteStatus == 'true'
                    ? false
                    : 'disabled'
                "
                controls-position="right"
                placeholder="rsync线程数"
                clearable
                :style="{ width: '100%' }"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item
              label="队列回收频率 (分)"
              prop="queueRecoveryInterval "
            >
              <el-input-number
                v-model="confPropInfo.queueRecoveryInterval"
                :min="0"
                controls-position="right"
                placeholder="回收队列磁盘空间占用频率5分钟一次"
                clearable
                :style="{ width: '100%' }"
              />
            </el-form-item>
          </el-col>
        </div>

        <div class="titleParame">控制台接口配置</div>
        <div style="display: inline-block; width: 100%">
          <el-col :span="8">
            <el-form-item label="控制台服务端口" prop="console_web_port">
              <el-input
                v-model="confPropInfo.console_web_port"
                placeholder="控制台服务端口"
                clearable
                :style="{ width: '100%' }"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="日志汇报接口" prop="console_restful_report">
              <el-input
                v-model="confPropInfo.console_restful_report"
                placeholder="日志汇报接口"
                clearable
                :style="{ width: '100%' }"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="测试连通接口" prop="console_restful_ping">
              <el-input
                v-model="confPropInfo.console_restful_ping"
                placeholder="测试连通接口"
                clearable
                :style="{ width: '100%' }"
              />
            </el-form-item>
          </el-col>
        </div>
        <!-- <div class="titleParame">
							脚本参数
						</div>	 -->
        <el-col v-show="false" :span="12">
          <el-form-item label="VIP执行脚本" prop="vip_script">
            <el-input
              v-model="confPropInfo.vip_script"
              placeholder="请输入VIP执行脚本"
              clearable
              :style="{ width: '100%' }"
            />
          </el-form-item>
        </el-col>
        <el-col v-show="false" :span="12">
          <el-form-item label="drbd执行脚本" prop="drbd_script">
            <el-input
              v-model="confPropInfo.drbd_script"
              placeholder="请输入drbd执行脚本"
              clearable
              :style="{ width: '100%' }"
            />
          </el-form-item>
        </el-col>
      </el-form>
    </el-row>
    <div slot="footer" class="footer">
      <el-button @click="systemParameClose">取消</el-button>
      <el-button
        v-hasPermi="['console:hessNode:edit']"
        type="primary"
        @click="handelConfirm"
      >保存</el-button>
    </div>
  </div>
</template>

<script>
import { getConfPropInfo, editConfPropInfo } from '@/api/console/group'
import global_ from '@/utils/Global'

export default {
  data() {
    return {
      // 系统参数 窗口是否打开
      systemParameVisible: false,
      // 系统参数 id
      confPropInfoId: '',
      // 系统参数
      confPropInfo: {
        agent_port: '',
        hess_json_file: '',
        httptest_connection_timeout: '',
        business_check_interval: '',
        ping_interval: '',
        rsync_enabled: 'true',
        rsync_transfer_workers: '',
        rsync_sleep_interval: '',
        rsync_account: '',
        ping_quick_check: true,
        agent_rpc_thread: 6,
        agent_rpc_timeout: 3000,
        tcp_connect_timeout: 2000
      },
      // 系统参数 判断条件
      rulesSys: {
        gateway_ip: [
          {
            required: true,
            message: '请输入网关IP地址',
            trigger: 'blur'
          },
          {
            pattern: global_.IpRegex,
            message: global_.IpRegex_msg,
            trigger: 'blur'
          }
        ],
        agent_port: [
          {
            required: true,
            message: '请输入agent的端口',
            trigger: 'blur'
          },
          {
            pattern: global_.PortRegex,
            message: '请输入合法_有效的 端口',
            trigger: 'blur'
          }
        ],
        hess_json_file: [
          {
            required: false,
            message: '请输入hess文件地址',
            trigger: 'blur'
          }
        ],
        agent_check_interval: [
          {
            required: true,
            message: '请输入agent检查的时间间隔',
            trigger: 'blur'
          }
        ],
        httptest_connection_timeout: [
          {
            required: true,
            message: '请输入http检查连接超时设置',
            trigger: 'blur'
          }
        ],

        business_check_interval: [
          {
            required: true,
            message: '请输入业务检查间隔',
            trigger: 'blur'
          }
        ],
        ping_interval: [
          {
            required: true,
            message: '请输入节点检查间隔',
            trigger: 'blur'
          }
        ],
        console_web_port: [
          {
            required: true,
            message: '请输入控制台服务端口',
            trigger: 'blur'
          }
        ],
        console_restful_report: [
          {
            required: true,
            message: '请输入日志汇报接口',
            trigger: 'blur'
          }
        ],
        console_restful_ping: [
          {
            required: true,
            message: '请输入测试连通接口',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created() {
    // this.initSystemParame()
  },
  methods: {
    // 打开查看系统参数
    initSystemParame(row) {
      (this.confPropInfoId = row.id), //
      getConfPropInfo({
        id: this.confPropInfoId
      })
        .then((response) => {
          this.confPropInfo = response.data
          // 页面是秒为单位 回显参数的时候需要转化为秒单位 要/1000
          this.confPropInfo.agent_check_interval = changesInfo(
            this.confPropInfo.agent_check_interval,
            0
          )
          this.confPropInfo.ping_interval = changesInfo(
            this.confPropInfo.ping_interval,
            0
          )
          this.confPropInfo.business_check_interval = changesInfo(
            this.confPropInfo.business_check_interval,
            0
          )

          this.confPropInfo.httptest_connection_timeout = changesInfo(
            this.confPropInfo.httptest_connection_timeout,
            0
          )

          this.confPropInfo.rsync_sleep_interval = changesInfo(
            this.confPropInfo.rsync_sleep_interval,
            0
          )
          this.confPropInfo.closeFileTime = changesInfo(
            this.confPropInfo.closeFileTime,
            0
          )
        })
        .catch((response) => {})

      // 毫秒转换秒
      function changesInfo(num, minNum) {
        let newNum
        if (!num) {
          num = 0
        }
        newNum = num / 1000
        return newNum < minNum ? minNum : newNum
      }
    },
    // 修改系统参数
    handelConfirm() {
      this.$refs['elForm'].validate((valid) => {
        if (!valid) return
        let confPropInfo = {}
        confPropInfo = JSON.parse(JSON.stringify(this.confPropInfo))

        // 页面是秒为单位 传参的时候需要转化为毫秒单位 要*1000
        confPropInfo.agent_check_interval =
          this.confPropInfo.agent_check_interval * 1000 + ''
        confPropInfo.ping_interval =
          this.confPropInfo.ping_interval * 1000 + ''
        confPropInfo.business_check_interval =
          this.confPropInfo.business_check_interval * 1000 + ''

        confPropInfo.httptest_connection_timeout =
          this.confPropInfo.httptest_connection_timeout * 1000 + ''

        confPropInfo.rsync_sleep_interval =
          this.confPropInfo.rsync_sleep_interval * 1000 + ''
        confPropInfo.closeFileTime =
          this.confPropInfo.closeFileTime * 1000 + ''

        const loading = this.$loading({
          lock: true,
          text: '加载中',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        })
        setTimeout(() => {
          loading.close()
        }, 20000)
        editConfPropInfo({
          id: this.confPropInfoId,
          confPropMap: confPropInfo
        })
          .then((response) => {
            loading.close()
            this.$message({
              message: '编辑成功',
              type: 'success'
            })
            this.$emit('systemParameClose')
          })
          .catch((err) => {
            loading.close()
          })
      })
    },
    // 关闭窗口
    systemParameClose() {
      clearInterval(this.time)
      this.$emit('systemParameClose')
    }
  }
}
</script>

<style>
.sysDialog .el-dialog__body {
  padding: 10px 50px;
}

.sysDialog #pane-sysDialog .footer {
  text-align: right;
}
</style>
